package com.bwie.controller;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.bwie.pojo.SpecGroup;
import com.bwie.pojo.SpecParam;
import com.bwie.service.SpecService;
import com.bwie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: day0423-pxx
 * @ClassName SpecController
 * @description:
 * @author: 段阳
 * @create: 2024-04-28 16:57
 **/
@RestController
@RequestMapping("item/spec")
public class SpecController {

    @Autowired
    private SpecService specService;

    @GetMapping("/groups/{categoryId}")
    public ResponseEntity<List<SpecGroup>> findSpecGroupList(@PathVariable(name = "categoryId") Long id){

        return specService.findSpecGroupList(id);
    }

    @GetMapping("/params")
    public ResponseEntity<List<SpecParam>> findSpecParamList(
            @RequestParam(value = "gid",required = false) Long gid,
            @RequestParam(value = "cid",required = false) Long cid,
            @RequestParam(value = "generic",required = false) Boolean generic,
            @RequestParam(value = "searching",required = false) Boolean searching

    ){


        List<SpecParam> list =  specService.findSpecParamList(gid,cid,generic,searching);
        if (CollectionUtils.isEmpty(list)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);
    }

    @PostMapping("/param")
    public R addSpecParam(@RequestBody SpecParam specParam){

        return specService.addSpecParam(specParam);
    }
    @PutMapping("/param")
    public R upSpecParam(@RequestBody SpecParam specParam){

        return specService.upSpecParam(specParam);
    }
    @DeleteMapping("/param/{id}")
    public R delSpecParam(@PathVariable(name = "id") Long id){

        return specService.delSpecParam(id);
    }

    @PostMapping("/group")
    public R addSpecGroup(@RequestBody SpecGroup specGroup){

        return specService.addSpecGroup(specGroup);
    }
    @PutMapping("/group")
    public R upSpecGroup(@RequestBody SpecGroup specGroup){

        return specService.upSpecGroup(specGroup);
    }
    @DeleteMapping("/group/{id}")
    public R delSpecGroup(@PathVariable(name = "id") Long id){

        return specService.delSpecGroup(id);
    }
}